<template>
  <view class="home">
    <!-- 搜索框 -->
    <my-search @click="toSearch"></my-search>
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay interval="2000" circular>
      <swiper-item v-for="(item, index) in lunboList" :key="index">
        <navigator :url="item.navigator_url" class="img">
          <image :src="item.image_src" mode="aspectFill"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="cate">
      <view @click="navTo(item.name)" class="item" v-for="(item, index) in cateList" :key="index">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor">
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层的标题 -->
        <image class="floor-title" :src="item.floor_title.image_src" mode=""></image>
        <!-- 楼层里的图片 -->
        <view class="imgbox">
          <view class="left">
            <image @click="toList(item.product_list[0].name)"
              :style="{ width: `${item.product_list[0].image_width}rpx` }" :src="item.product_list[0].image_src"
              mode="widthFix">
            </image>
          </view>
          <view class="right">
            <block v-for="(subitem, subindex) in item.product_list" :key="subindex">
              <image @click="toList(subitem.name)" v-if="subindex !== 0" :style="{ width: `${subitem.image_width}rpx` }"
                :src="subitem.image_src" mode="widthFix"></image>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import mymixins from '../../mixins/mymixins.js'
  export default {
    mixins: [mymixins],
    data() {
      return {
        // 轮播图的数据源
        lunboList: [],
        // 分类导航的数据
        cateList: [],
        // 楼层数据
        floorList: []
      };
    },
    onLoad() {
      // 获取轮播图的数据
      this.getLunboList()
      // 获取分类导航的数据
      this.getCateListe()
      // 获取楼层数据
      this.getFloorList()
    },
    methods: {
      toSearch () {
        uni.navigateTo({
          url: '/pages/search/search'
        })
      },
      // 跳转到对应的列表页面
      toList(name) {
        uni.navigateTo({
          url: `/pages/list/list?key=${name}`
        })
      },
      // 得到楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.floorList = res.message
      },
      // 点击导航进行跳转
      navTo(name) {
        // 区别是否是分类
        if (name === '分类') {
          // 跳转到分类页面
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 得到分类导航数据
      async getCateListe() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems').catch(err => err)
        // 判断是否成功
        if (res.meta.status !== 200) return uni.$showMsg()
        // 保存数据
        this.cateList = res.message
      },
      // 得到轮播图数据
      async getLunboList() {
        // 发送网络请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata').catch(error => error)
        // 判断请求是否成功
        if (res.meta.status !== 200) return uni.$showMsg()
        // 保存数据
        this.lunboList = res.message
      }
    }
  }
</script>

<style lang="scss">
  .home {
    .img {
      height: 330rpx;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .cate {
      display: flex;
      height: 200rpx;
      align-items: center;

      .item {
        text-align: center;
        width: 25%;

        image {
          width: 130rpx;
          height: 130rpx;
        }
      }
    }

    .floor {
      .floor-item {
        padding: 10rpx;

        .floor-title {
          width: 100%;
          height: 60rpx;
        }

        .imgbox {
          display: flex;

          .right {
            display: flex;
            flex-wrap: wrap;

            image {
              margin-left: 10rpx;
            }
          }
        }
      }
    }
  }
</style>
